using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HappyFrogXna.ScreenSystem;
using FarseerPhysics;
using FarseerPhysics.Dynamics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
#if WINDOWS_PHONE
using Microsoft.Xna.Framework.Input.Touch;
#endif
using Microsoft.Xna.Framework.Media;
using HappyFrogXna.DrawingSystem;
using System.Diagnostics;
using HappyFrogXna.Roles;
using HappyFrogXna.Level;

namespace HappyFrogXna
{
    
    /// <summary>
    /// the main screen of game play
    /// </summary>
    public class GameMainScreen : PhysicsGameScreen
    {
        bool _gamePlaying = true;

        // game logic
        GameLogicController _gameLogicController;

        // scene build
        SceneBuildController _sceneBuildController;

        // Screen dragging handle
        bool beginDragBackground = false;
        Vector2 lastDragBackgroundPos = Vector2.Zero;

        public GameMainScreen() :
            base()
        {
            //ScreenPanel screenPanel = new ScreenPanel();
            //ScreenContentProvider.Instance.Game.Components.Add(screenPanel);

            //ScreenContentProvider.Instance.Game.GraphicsDevice.RenderState.AlphaBlendEnable = true;
            //ScreenContentProvider.Instance.Game.GraphicsDevice.RenderState.SourceBlend = Blend.BothSourceAlpha;
            //ScreenContentProvider.Instance.Game.GraphicsDevice.RenderState.DestinationBlend = Blend.SourceAlpha;
            //BlendState blendState = new BlendState();
            //blendState.AlphaDestinationBlend = Blend.DestinationAlpha;
            //blendState.AlphaSourceBlend = Blend.SourceAlpha;
            //blendState.ColorDestinationBlend = Blend.DestinationAlpha;
            //blendState.AlphaSourceBlend = Blend.SourceAlpha;
            //ScreenContentProvider.Instance.Game.GraphicsDevice.BlendState = blendState;
        }

        public override void LoadContent()
        {
            base.LoadContent();

            EnableCameraControl = false; // Disable keyboard camera control
            HasCursor = true; // Show cursor
            EnableCursorDrag = false; // Disable cursor drag

            World.Gravity = new Vector2(0, 10f);

            if (_gamePlaying)
            {
                _gameLogicController = new GameLogicController(World, Camera);
                _gameLogicController.LoadContent();
            }
            else
            {
                _sceneBuildController = new SceneBuildController(World, Camera);
                _sceneBuildController.LoadContent();
                _sceneBuildController.MoveLeft = moveCameraLeft;
                _sceneBuildController.MoveRight = moveCameraRight;
            }
        }

        private void moveCameraLeft(float value)
        {
            Camera.Position -= new Vector2(value, 0);
        }

        private void moveCameraRight(float value)
        {
            Camera.Position += new Vector2(value, 0);
        }

        public override void HandleInput(InputHelper input, GameTime gameTime)
        {
            Vector2 position = Camera.ConvertScreenToWorld(input.Cursor);
            
            if (input.IsMenuCancel())
            {
                Exit();
            }

            bool handledByController;
            if (_gamePlaying)
                handledByController = _gameLogicController.HandleInput(input, gameTime);
            else
                handledByController = _sceneBuildController.HandleInput(input, gameTime);

            if (!handledByController)
            {
                if (input.IsNewMouseButtonPress(MouseButtons.LeftButton))
                {
                    beginDragBackground = true;
                    lastDragBackgroundPos = position;
                }
                else if (input.IsNewMouseButtonRelease(MouseButtons.LeftButton))
                {
                    beginDragBackground = false;
                    lastDragBackgroundPos = Vector2.Zero;
                }
                else
                {
                    if (beginDragBackground)
                    {
                        float moveX = (lastDragBackgroundPos.X - position.X) * 20f * (float)gameTime.ElapsedGameTime.TotalSeconds;
                        if (Math.Abs(moveX) > 0.1f)
                        {
                            Camera.MoveCamera(new Vector2(moveX, 0));
                            lastDragBackgroundPos = position;
                        }
                    }
                }
            }

            base.HandleInput(input, gameTime);
        }
        
        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            if (_gamePlaying)
                _gameLogicController.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
            else
                _sceneBuildController.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);

            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
        }

        public override void Draw(GameTime gameTime)
        {
            if (_gamePlaying)
                _gameLogicController.Draw(gameTime);
            else
                _sceneBuildController.Draw(gameTime);

            base.Draw(gameTime);
        }
    }
}
